﻿using Pay.Core;
using Pay.Data;
using Pay.DTO;
using Pay.IService;
using Pay.Service.Cache;
using Pay.TO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Pay.Service {
    public class PlayingTeamService : ServiceBase, IPlayingTeamService, IDisposable {
        private IUnitOfWork _iUnitOfWork;
        public PlayingTeamService(IUnitOfWork iUnitOfWork) : base(iUnitOfWork) {
            _iUnitOfWork = iUnitOfWork;
        }

        /// <summary>
        /// 获得队伍列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public PagedResult<playing_team_dto> GetPlayingTeams(PlayingTeamQuery query) {
            var filter = PredicateBuilder.True<PlayingTeam>();
            if (!string.IsNullOrEmpty(query.title)) {
                filter = filter.And(c => c.Title == query.title);
            }
            var result = context.PlayingTeam.FromCachePaged<PlayingTeam, playing_team_dto>(query.page, query.size, filter, orderBy => orderBy.OrderByDescending(o => o.Id));
            return result;
        }

        /// <summary>
        /// 获得队伍
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public playing_team_dto GetPlayingTeam(long id) {
            return context.PlayingTeam.Find(id).ToDtoObject<playing_team_dto>();
        }

        /// <summary>
        /// 新增队伍
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public int AddPlayingTeam(playing_team_dto dto) {
            PlayingTeam model = new PlayingTeam();
            dto.ToDtoObject(model);
            context.PlayingTeam.Add(model);
            int result = _iUnitOfWork.Commit();
            return result;
        }

        /// <summary>
        /// 更新队伍
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public int UpdatePlayingTeam(playing_team_dto dto) {
            PlayingTeam model = context.PlayingTeam.FindById(dto.id);
            dto.ToDtoObject(model);
            int result = _iUnitOfWork.Commit();
            return result;
        }

        /// <summary>
        /// 获得所有队伍的下拉选择数据
        /// </summary>
        /// <returns></returns>
        public List<DropdownItem> GetTeamDropdownItems() {
            var result = new List<DropdownItem>();
            var list = context.PlayingTeam.ToList();
            if (list != null && list.Count() > 0) {
                foreach (var team in list) {
                    result.Add(new DropdownItem() { Text = team.Title, Value = team.Id.ToString() });
                }
            }
            return result;
        }


    }
}
